package com.lzb.permanentcalendar.adapter;

import android.graphics.Color;
import android.util.Log;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.basic.ProviderMultiAdapter;
import com.lzb.permanentcalendar.bean.ScheduleBean;
import com.lzb.permanentcalendar.utils.DateCalculator;
import com.othershe.calendarview.bean.JrBean;

import java.text.SimpleDateFormat;

public class ScheduleAdapter extends ProviderMultiAdapter<ScheduleBean> {

    public ScheduleAdapter() {
        addItemProvider(new ScheduleProvider());
    }

    private class ScheduleProvider extends BaseItemProvider<ScheduleBean> {
        @Override
        public int getItemViewType() {
            return ProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_schedule;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, ScheduleBean bean) {
            baseViewHolder.setText(R.id.tvScheduleItemTitle, bean.getTitle());
            baseViewHolder.setText(R.id.tvScheduleItemDate, TimeUtils.millis2String(bean.getStartTime(), new SimpleDateFormat("MM月dd日")));
            long today = 0;
            if (bean.isAllDay()) {
                baseViewHolder.setText(R.id.tvScheduleItemTime, getContext().getString(R.string.all_day));
                today = TimeUtils.string2Millis(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")),new SimpleDateFormat("yyyy-MM-dd"));
            } else {
                baseViewHolder.setText(R.id.tvScheduleItemTime, TimeUtils.millis2String(bean.getStartTime(), new SimpleDateFormat("HH:mm")));
                today = TimeUtils.getNowMills();
            }
            if (today > bean.getEndTime()) {
                baseViewHolder.setTextColor(R.id.tvScheduleItemNum, Color.parseColor("#999999"));
                baseViewHolder.setTextColor(R.id.tvScheduleItemTitle, Color.parseColor("#999999"));
                baseViewHolder.setTextColor(R.id.tvScheduleItemTime, Color.parseColor("#999999"));
                baseViewHolder.setTextColor(R.id.tvScheduleItemDate, Color.parseColor("#999999"));
                baseViewHolder.setText(R.id.tvScheduleItemNum, getContext().getString(R.string.expired));
            } else {
                baseViewHolder.setTextColor(R.id.tvScheduleItemNum, Color.parseColor("#AD2419"));
                baseViewHolder.setTextColor(R.id.tvScheduleItemTitle, Color.parseColor("#333333"));
                baseViewHolder.setTextColor(R.id.tvScheduleItemTime, Color.parseColor("#AD2419"));
                baseViewHolder.setTextColor(R.id.tvScheduleItemDate, Color.parseColor("#333333"));
                int num = DateCalculator.getDaysBetweenTimestamps(bean.getStartTime(), today);
                if (num > 0) {
                    baseViewHolder.setText(R.id.tvScheduleItemNum, getContext().getString(R.string.after_day_num, num));
                } else if (num == 0) {
                    baseViewHolder.setText(R.id.tvScheduleItemNum, getContext().getString(R.string.today));
                } else {
                    baseViewHolder.setText(R.id.tvScheduleItemNum, getContext().getString(R.string.before_day_num, Math.abs(num)));
                }
            }
        }
    }

}
